home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE23 / CLINIC / MOMU.PAS < prev    next >
Pascal/Delphi Source File  |  1997-04-28  |  4KB  |  137 lines

  1. unit MOMU;
  2.  
  3. interface
  4.  
  5. uses
  6.   WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls;
  8.  
  9. type
  10.   TMOMForm = class(TForm)
  11.     SpeedButton1: TSpeedButton;
  12.     SpeedButton2: TSpeedButton;
  13.     SpeedButton3: TSpeedButton;
  14.     SpeedButton4: TSpeedButton;
  15.     SpeedButton5: TSpeedButton;
  16.     SpeedButton6: TSpeedButton;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure SpeedButton1Click(Sender: TObject);
  19.     procedure SpeedButton2Click(Sender: TObject);
  20.     procedure SpeedButton6Click(Sender: TObject);
  21.     procedure SpeedButton3Click(Sender: TObject);
  22.     procedure SpeedButton4Click(Sender: TObject);
  23.     procedure SpeedButton5Click(Sender: TObject);
  24.   private
  25.     { Private declarations }
  26.   public
  27.     { Public declarations }
  28.   end;
  29.  
  30. var
  31.   MOMForm: TMOMForm;
  32.  
  33. implementation
  34.  
  35. {$R *.DFM}
  36.  
  37. procedure ActivateWindow(Caption, ClassName: PChar);
  38. var
  39.   FormWnd, AppWnd: HWnd;
  40. begin
  41.   FormWnd := FindWindow(ClassName, Caption);
  42.   if FormWnd = 0 then
  43.     raise Exception.Create('Cannot find window');
  44. {$ifdef Win32}
  45.   { Having found the form, now find the Application window }
  46.   { Can't reliably use GetParent, so... }
  47.   AppWnd := GetWindowLong(FormWnd, gwl_HWndParent);
  48.   { Tell the Delphi Application window to pop up in case it }
  49.   { is minimised. Bear in mind that FindWindow only works on }
  50.   { top-level windows, not child windows. Delphi forms are }
  51.   { top-level popup windows which have parents, so the following }
  52.   { check should only try and restore a parent window if it }
  53.   { is a Delphi app }
  54.   if (AppWnd <> HWnd_Desktop) and IsIconic(AppWnd) then
  55.     SendMessage(AppWnd, wm_SysCommand, sc_Restore, 0);
  56.   { Tell the form window to pop up if it is minimised }
  57.   if IsIconic(FormWnd) then
  58.     SendMessage(FormWnd, wm_SysCommand, sc_Restore, 0);
  59.   { Make the target form be in front and active }
  60.   SetForegroundWindow(FormWnd);
  61. {$else}
  62.   AppWnd := GetWindowWord(FormWnd, gww_HWndParent);
  63.   if (AppWnd <> HWnd_Desktop) and IsIconic(AppWnd) then
  64.     SendMessage(AppWnd, wm_SysCommand, sc_Restore, 0);
  65.   if IsIconic(FormWnd) then
  66.     SendMessage(FormWnd, wm_SysCommand, sc_Restore, 0);
  67.   BringWindowToTop(FormWnd);
  68. {$endif}
  69. end;
  70.  
  71. procedure RunApp(AppName: String);
  72. {$ifdef Win32}
  73. var
  74.   SI: TStartupInfo;
  75.   PI: TProcessInformation;
  76. begin
  77.   GetStartupInfo(SI);
  78.   if not CreateProcess(nil, PChar(AppName), nil,
  79.            nil, False, 0, nil, nil, SI, PI) then
  80. {$else}
  81. var
  82.   Buf: array[0..255] of Char;
  83. begin
  84.   if WinExec(StrPCopy(Buf, AppName), sw_ShowNormal) < HInstance_Error then
  85. {$endif}
  86.     raise Exception.Create('Failed to run ' + AppName)
  87. end;
  88.  
  89. procedure RunOrSwitchToApp(const AppPath: String; ClassName: PChar);
  90. begin
  91.   try
  92.     ActivateWindow(nil, ClassName)
  93.   except
  94.     RunApp(AppPath)
  95.   end
  96. end;
  97.  
  98. procedure TMOMForm.FormCreate(Sender: TObject);
  99. begin
  100. {$ifdef Win32}
  101.   BorderStyle := bsToolWindow;
  102.   ClientHeight := 25;
  103. {$endif}
  104. end;
  105.  
  106. procedure TMOMForm.SpeedButton1Click(Sender: TObject);
  107. begin
  108.   RunOrSwitchToApp('c:\excel\excel.exe', 'XLMAIN')
  109. end;
  110.  
  111. procedure TMOMForm.SpeedButton2Click(Sender: TObject);
  112. begin
  113.   RunOrSwitchToApp('c:\word6\winword.exe', 'OpusApp')
  114. end;
  115.  
  116. procedure TMOMForm.SpeedButton6Click(Sender: TObject);
  117. begin
  118.   RunOrSwitchToApp('c:\delphi\delphi.exe', 'TAppBuilder')
  119. end;
  120.  
  121. procedure TMOMForm.SpeedButton3Click(Sender: TObject);
  122. begin
  123.   RunOrSwitchToApp('calc.exe', 'SciCalc')
  124. end;
  125.  
  126. procedure TMOMForm.SpeedButton4Click(Sender: TObject);
  127. begin
  128.   RunOrSwitchToApp('notepad.exe', 'Notepad')
  129. end;
  130.  
  131. procedure TMOMForm.SpeedButton5Click(Sender: TObject);
  132. begin
  133.   Close
  134. end;
  135.  
  136. end.
  137.